package com.sl.au.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Account;
import com.sl.au.entity.EXDELoginInfo;
import com.sl.au.entity.EventToEvaluateUser;
import com.sl.au.entity.Role;
import com.sl.au.entity.User;
import com.sl.au.entity.ViewForUserScoreAnalysis;
import com.sl.au.repository.AccountRepository;
import com.sl.au.repository.EXDELoginInfoRepository;
import com.sl.au.repository.EventToEvaluateUserRepository;
import com.sl.au.repository.UserRepository;
import com.sl.au.service.AccountService;
import com.sl.au.service.EventToEvaluateUserService;
import com.sl.au.service.ModuleService;

@Service("eventToEvaluateUserService")
@Transactional
public class EventToEvaluateUserServiceImpl extends BaseServiceImpl implements EventToEvaluateUserService {

	@Autowired
	private EventToEvaluateUserRepository eventToEvaluateUserRepository;
	@Resource
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	@Override
	public EventToEvaluateUserRepository getRepository() {
		return this.eventToEvaluateUserRepository;
	}

	@Override
	public List<EventToEvaluateUser> getPageByDate(int start, int length, String uId, String key, Date begin, Date end, String dataType, String dataId, String orderName, Direction dir) {
		Page<EventToEvaluateUser> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		
		if(dataType.equals("part")) {
			if(dataId.equals("part")) {
				if (StringUtils.hasText(key)) {
					pages = eventToEvaluateUserRepository.findPartByDateAndKey(begin, end, uId, key, pageable);
				}else {
					pages = eventToEvaluateUserRepository.findPartByDate(begin, end, uId, pageable);
				}
			}else if(dataId.equals("notpart")){
				if (StringUtils.hasText(key)) {
					pages = eventToEvaluateUserRepository.findNotPartByDateAndKey(begin, end, uId, key, pageable);
				}else {
					pages = eventToEvaluateUserRepository.findNotPartByDate(begin, end, uId, pageable);
				}
			}else {
				if (StringUtils.hasText(key)) {
					pages = eventToEvaluateUserRepository.findPartIdByDateAndKey(begin, end, uId, dataId, key, pageable);
				}else {
					pages = eventToEvaluateUserRepository.findPartIdByDate(begin, end, uId, dataId, pageable);
				}
			}
		}else if(dataType.equals("event")){
			if (StringUtils.hasText(key)) {
				pages = eventToEvaluateUserRepository.findEventIdByDateAndKey(begin, end, uId, dataId, key, pageable);
			}else {
				pages = eventToEvaluateUserRepository.findEventIdByDate(begin, end, uId, dataId, pageable);
			}
		}else if(dataType.equals("division")){
			if (StringUtils.hasText(key)) {
				pages = eventToEvaluateUserRepository.findDivisionIdByDateAndKey(begin, end, uId, dataId, key, pageable);
			}else {
				pages = eventToEvaluateUserRepository.findDivisionIdByDate(begin, end, uId, dataId, pageable);
			}
		}else if(dataType.equals("grid")){
			if (StringUtils.hasText(key)) {
				pages = eventToEvaluateUserRepository.findGridIdByDateAndKey(begin, end, uId, dataId, key, pageable);
			}else {
				pages = eventToEvaluateUserRepository.findGridIdByDate(begin, end, uId, dataId, pageable);
			}
		}
		return pages.getContent();
	}

	@Override
	public int getCountByDate(String uId, String key, Date begin, Date end, String dataType, String dataId) {
		Pageable pageable = new PageRequest(0 , Integer.MAX_VALUE);
		int count = 0;
		if(dataType.equals("part")) {
			if(dataId.equals("part")) {
				if (StringUtils.hasText(key)) {
					count = eventToEvaluateUserRepository.findPartByDateAndKey(begin, end, uId, key, pageable).getContent().size();
				}else {
					count = eventToEvaluateUserRepository.findPartByDate(begin, end, uId, pageable).getContent().size();
				}
			}else if(dataId.equals("notpart")){
				if (StringUtils.hasText(key)) {
					count = eventToEvaluateUserRepository.findNotPartByDateAndKey(begin, end, uId, key, pageable).getContent().size();
				}else {
					count = eventToEvaluateUserRepository.findNotPartByDate(begin, end, uId, pageable).getContent().size();
				}
			}else {
				if (StringUtils.hasText(key)) {
					count = eventToEvaluateUserRepository.findPartIdByDateAndKey(begin, end, uId, dataId, key, pageable).getContent().size();
				}else {
					count = eventToEvaluateUserRepository.findPartIdByDate(begin, end, uId, dataId, pageable).getContent().size();
				}
			}
		}else if(dataType.equals("event")){
			if (StringUtils.hasText(key)) {
				count = eventToEvaluateUserRepository.findEventIdByDateAndKey(begin, end, uId, dataId, key, pageable).getContent().size();
			}else {
				count = eventToEvaluateUserRepository.findEventIdByDate(begin, end, uId, dataId, pageable).getContent().size();
			}
		}else if(dataType.equals("division")){
			if (StringUtils.hasText(key)) {
				count = eventToEvaluateUserRepository.findDivisionIdByDateAndKey(begin, end, uId, dataId, key, pageable).getContent().size();
			}else {
				count = eventToEvaluateUserRepository.findDivisionIdByDate(begin, end, uId, dataId, pageable).getContent().size();
			}
		}else if(dataType.equals("grid")){
			if (StringUtils.hasText(key)) {
				count = eventToEvaluateUserRepository.findGridIdByDateAndKey(begin, end, uId, dataId, key, pageable).getContent().size();
			}else {
				count = eventToEvaluateUserRepository.findGridIdByDate(begin, end, uId, dataId, pageable).getContent().size();
			}
		}
		return count;
	}

	@Override
	public double getUserScoreByDate(String userId, Date begin, Date end) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("begin", begin);
		params.put("end", end);
		params.put("userId", userId);
		double score = 0;
		String SQL = "select convert (100 - ifnull(sum(a.scale),0),decimal(10,2)) from sl_wcsi_EventToEvaluateUser a "
				+ " where a.registerUser = :userId and a.createTime >:begin and a.createTime <:end ";
		score = namedParameterJdbcTemplate.queryForObject(SQL,params,Double.class);
		
		SQL = "select convert (ifnull(sum(a.scale),0),decimal(10,2)) from sl_wcsi_eventprocesstoevaluateuser a "
				+ " where a.registerUser = :userId and a.accTime >:begin and a.accTime <:end ";
		score-=namedParameterJdbcTemplate.queryForObject(SQL,params,Double.class);
		
		return score;
	}
	@Override
	public double getUserMarkScoreByDate(String userId, Date begin, Date end) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("begin", begin);
		params.put("end", end);
		params.put("userId", userId);
		double score = 0;
		String SQL = "select convert (ifnull(sum(a.scale),0),decimal(10,2)) from sl_wcsi_EventToEvaluateUser a "
				+ " where a.registerUser = :userId and a.createTime >:begin and a.createTime <:end ";
		score = namedParameterJdbcTemplate.queryForObject(SQL,params,Double.class);
		SQL = "select convert (ifnull(sum(a.scale),0),decimal(10,2)) from sl_wcsi_eventprocesstoevaluateuser a "
				+ " where a.registerUser = :userId and a.accTime >:begin and a.accTime <:end ";
		score +=namedParameterJdbcTemplate.queryForObject(SQL,params,Double.class);
		return score;
	}



}
